package cn.tedu.management.portal.controller;


import cn.tedu.management.portal.ex.ServiceException;
import cn.tedu.management.portal.mapper.AttendanceMapper;
import cn.tedu.management.portal.model.Attendance;
import cn.tedu.management.portal.service.impl.AttendanceServiceImpl;
import cn.tedu.management.portal.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tedu.cn
 * @since 2020-12-08
 */
@RestController
@RequestMapping("/portal/attendance")
public class AttendanceController {

    @Autowired
    AttendanceMapper attendanceMapper;

    @GetMapping("/delete")
    public R<Void> delete(int id){
        attendanceMapper.deleteById(id);
        return R.ok();
    }


    @Autowired
    AttendanceServiceImpl attendanceService;

    // http://localhost:8080/portal/attendance/update?staffId=9&absenceCount=30&holiday=40
    @GetMapping("/update")
    public R<Void> updateAttendance(int staffId,double absenceCount,int holiday){

        try {
            attendanceService.updateAttendance(staffId,absenceCount,holiday);
            return R.ok();
        }catch (ServiceException e){
            return R.failure(R.State.ERR_INSERT,e);
        }


    }


    // http://localhost:8080/portal/attendance/find?id=1
    @GetMapping("/find")
    public R<Attendance> find(int id){
        Attendance attendance = attendanceMapper.selectById(id);
        return R.ok(attendance);
    }


}
